/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.sample.network.swing;

import java.util.HashMap;
import java.util.Map;

import nemos.sample.network.FrameworkDescription;
import nemos.util.swing.IRowConverter;

/**
 * ConnectedConverter : The connected framework models converter.
 * 
 * @author Romain DUBOIS
 */
public class ConnectedConverter implements IRowConverter<String> {

    /** The default string */
    private static final String DEFAULT_STRING = "?"; //$NON-NLS-1$

    /** The frameworks descriptions */
    private final Map<String, FrameworkDescription> mDescriptions;

    /**
     * Constructor.
     */
    public ConnectedConverter() {
        super();

        mDescriptions = new HashMap<String, FrameworkDescription>();
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.util.swing.IRowConverter#getColumnType(int)
     */
    @Override
    public Class<?> getColumnType(@SuppressWarnings("unused") final int pColumn) {
        return String.class;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.util.swing.IRowConverter#getValue(java.lang.Object, int)
     */
    @Override
    public String getValue(final String pObject, final int pColumn) {
        final FrameworkDescription lDesc = mDescriptions.get(pObject);

        String lResult;
        if (pColumn == 0) {
            lResult = pObject;
        } else if (lDesc == null) {
            lResult = DEFAULT_STRING;
        } else {
            switch (pColumn) {
                case 1:
                    lResult = lDesc.getSystemUserName();
                    break;

                case 2:
                    lResult = lDesc.getHostName();
                    break;

                case 3:
                    lResult = lDesc.getOsName() + '/' + lDesc.getOsArch() + '-'
                            + lDesc.getOsVersion();
                    break;

                default:
                    lResult = null;
            }
        }
        return lResult;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.util.swing.IRowConverter#getColumnCount()
     */
    @Override
    public int getColumnCount() {
        return 4;
    }

    /**
     * Set the description of a remote framework.
     * 
     * @param pUuid
     *            The framework UUID
     * @param pDescription
     *            The description
     */
    public void setDescription(final String pUuid, final FrameworkDescription pDescription) {
        mDescriptions.put(pUuid, pDescription);
    }

    /**
     * Remove a remote framework's description.
     * 
     * @param pUuid
     *            The framework UUID
     */
    public void removeDescription(final String pUuid) {
        mDescriptions.remove(pUuid);
    }
}
